#include "Common.h"

initSing(ChatHandler);

ChatHandler::ChatHandler()
{
	InitCmds();
}

bool ChatHandler::BuildChatPacket(char* message, ui32 senderguid, ui32 type, ui32 language, RealmPacket* out)
{
	if(!out)
		return false;
	*out << ui8(type) << language;
	*out << ui64(senderguid) << ui32(0) << ui64(senderguid) << ui32(strlen(message) + 1) << message << ui8(0);
	out->SetSize(out->GetSize());
	return true;
}

void ChatHandler::BuildSystemPacket(char* message, RealmPacket* out)
{
	if(!out)
		return;
	*out << ui8(CHAT_MSG_SYSTEM) << ui32(0) << ui64(0) << ui32(0) << ui64(0);
	*out << ui32(strlen(message) + 1) << message << ui8(0);
	return;
}

void ChatHandler::HandleChatMessage(ChatMessage& cmsg, RealmPacket* remaining)
{
	if(!cmsg.sender)
		return;
	switch(cmsg.type)
	{
	case CHAT_MSG_SAY:
	case CHAT_MSG_YELL:
		*remaining >> cmsg.message;
		break;
	case CHAT_MSG_CHANNEL:
		{
			char chnl[255];
			*remaining >> chnl >> cmsg.message;
			RealmPacket pack(SMSG_MESSAGECHAT, 0);
			pack << ui8(CHAT_MSG_CHANNEL) << ui32(0) << cmsg.sender->GetGUID() << ui32(0) << chnl << cmsg.sender->GetGUID();
			pack << ui32(strlen(cmsg.message) + 1) << cmsg.message << ui8(0);
			pack.SetSize(pack.GetSize());
			cmsg.sender->GetSession()->SendPacket(&pack);
			return;
		}
	case CHAT_MSG_WHISPER:
		{
			char receiver[255];
			*remaining >> receiver >> cmsg.message;
			Player* plr = miscmgr->GetPlayer((const char*)receiver);
			if(!plr)
			{
				RealmPacket data(SMSG_CHAT_PLAYER_NOT_FOUND);
				data << receiver;
				cmsg.sender->GetSession()->SendPacket(&data);
				return;
			}
			RealmPacket pack(SMSG_MESSAGECHAT);
			BuildChatPacket(cmsg.message, (ui32)plr->GetGUID(), cmsg.type, cmsg.language, &pack);
			plr->GetSession()->SendPacket(&pack);
			pack.Clear();
			pack.SetOpcode(SMSG_MESSAGECHAT);
			BuildChatPacket(cmsg.message, (ui32)cmsg.sender->GetGUID(), CHAT_MSG_WHISPER_INFORM, cmsg.language, &pack);
			cmsg.sender->GetSession()->SendPacket(&pack);
			return;
		}
	default:
		return;
	}
	if(ParseMessage(cmsg))
		return;
	RealmPacket pack(SMSG_MESSAGECHAT, 0);
	BuildChatPacket(cmsg.message, (ui32)cmsg.sender->GetGUID(), cmsg.type, cmsg.language, &pack);
	cmsg.sender->GetSession()->SendPacket(&pack);
}

bool ChatHandler::ParseMessage(ChatMessage& cmsg)
{
	switch(cmsg.type)
	{
	case CHAT_MSG_SAY:
	case CHAT_MSG_YELL:
		break;
	default:
		return false;
	}
	char* cmd = new char[strlen(cmsg.message) + 1];
	strcpy(cmd, cmsg.message);
	if(*cmd != '.')
		return false;
	++cmd;
	char* cm = strtok(cmd, " ");
	if(!cm)
		return false;
	char* args = strtok(NULL, "\0");
	ChatCommand* comd = FindRegularCommand(cm);
	if(comd)
	{
		HandleChatCommand(comd, cmsg.sender, args);
		return true;
	}
	if(!sScriptMgr->ParseCommand(cm, args, cmsg.sender))
	{
		cmsg.sender->RedSystemMessage("Der Befehl '%s' wurde nicht gefunden!", cm);
	}
	return true;
}

void ChatHandler::InitCmds()
{
	//	Implement your own ChatTables here with the commands
	ChatCommandTable* npccmds = new ChatCommandTable;
	npccmds->InsertCmd(&ChatHandler::HandleNPCSpawnCmd, "spawn", "Spawns a npc with the specified entry", 'm');
	npccmds->InsertCmd(&ChatHandler::HandleNPCInfoCmd, "info", "Retrieves some information about the npc", 'm');
	npccmds->InsertCmd(&ChatHandler::HandleNPCDeleteCmd, "delete", "Deletes the selected creature", 'm');
	npccmds->InsertCmd(&ChatHandler::HandleNPCFlagCmd, "flag", "Changes the npcflag of the selected creature", 'm');
	npccmds->InsertCmd(&ChatHandler::HandleNPCFactionCmd, "faction", "Changes the faction of the selected creature", 'm');
	npccmds->InsertCmd(&ChatHandler::HandleNPCEmoteCmd, "emote", "Changes the emotestate of the selected creature", 'm');
	npccmds->InsertCmd(&ChatHandler::HandleNPCMovespCmd, "movespawn", "Changes the spawnpoint of the selected creature", 'm');
	npccmds->InsertCmd(&ChatHandler::HandleNpcPower, "power", "", 'm');

	ChatCommandTable* wpcmds = new ChatCommandTable;
	wpcmds->InsertCmd(&ChatHandler::HandleWayPointAdd, "add", "Adds a waypoint to the npc", 'm');
	wpcmds->InsertCmd(&ChatHandler::HandleWayPointActivate, "activate", "Activates the waypoints", 'm');

	ChatCommandTable* debugcmds = new ChatCommandTable;
	debugcmds->InsertCmd(&ChatHandler::HandleDebugMove, "move", "Moves the selected NPC to your position (but wont save the position!)", 'm');

	ChatCommandTable* cheatcmds = new ChatCommandTable;
	cheatcmds->InsertCmd(&ChatHandler::HandleCheatFly, "fly", "Allows you to fly!", 'm');

	ChatCommandTable* modifycmds = new ChatCommandTable;
	modifycmds->InsertCmd(&ChatHandler::HandleModifySpeed, "speed", "Changes the speed to the selected value (0 - 255)", 'm');
	modifycmds->InsertCmd(&ChatHandler::HandleModifyMoney, "money", "Changes the money of the player", 'm');
	
	ChatCommandTable* recallcmds = new ChatCommandTable;
	recallcmds->InsertCmd(&ChatHandler::HandleRecallAdd, "add", "Adds a location to the database. Usage: .recall add <name>", 'm');
	recallcmds->InsertCmd(&ChatHandler::HandleRecallPort, "port", "Ports you to the location", 'm');

	ChatCommandTable* gocmds = new ChatCommandTable;
	gocmds->InsertCmd(&ChatHandler::HandleGameObjectSelect, "select", "", 'm');
	gocmds->InsertCmd(&ChatHandler::HandleGameObjectByte2, "bytes2", "", 'm');
	gocmds->InsertCmd(&ChatHandler::HandleGameObjectFlags, "flags", "", 'm');
	gocmds->InsertCmd(&ChatHandler::HandleGameObjectByte3, "bytes3", "", 'm');
	gocmds->InsertCmd(&ChatHandler::HandleGameObjectReloadTele, "reloadtele", "Reload the teleport-storage", 'm');
	
	//	Do not implement own ChatTables after this!
	ChatCommandTable* maincmds = new ChatCommandTable;
	
	//	Add own main-commands here (like thus to show your own chattable)
	maincmds->InsertCmd(&ChatHandler::HandleWorldPortCmd, "worldport", "Usage: .worldport <mapid> <x> <y> <z>", 'm');
	maincmds->InsertCmd(&ChatHandler::HandleAnnounce, "announce", "Announces the message", 'm');
	maincmds->InsertCmd(0, "npc", "", 'm', npccmds);
	maincmds->InsertCmd(0, "waypoint", "", 'm', wpcmds);
	maincmds->InsertCmd(0, "debug", "", 'm', debugcmds);
	maincmds->InsertCmd(0, "modify", "", 'm', modifycmds);
	maincmds->InsertCmd(0, "cheat", "", 'm', cheatcmds);
	maincmds->InsertCmd(0, "recall", "", 'm', recallcmds);
	maincmds->InsertCmd(0, "go", "", 'm', gocmds);

	//	Do not add more main-commands after this!
	CommandTableStorage.insert(maincmds);

	//	Add your own ChatTables to the storage
	CommandTableStorage.insert(npccmds);
	CommandTableStorage.insert(wpcmds);
	CommandTableStorage.insert(debugcmds);
	CommandTableStorage.insert(cheatcmds);
	CommandTableStorage.insert(modifycmds);
	CommandTableStorage.insert(recallcmds);
	CommandTableStorage.insert(gocmds);
}

void ChatCommandTable::InsertCmd(bool (ChatHandler::*cp)(Player*, char*), char* name, char* helptext, char lvl, ChatCommandTable* subcmds)
{
	ChatCommand* commi = new ChatCommand();
	commi->ChatProc = cp;
	commi->InternalName = name;
	commi->RequiredLevel = lvl;
	commi->subcommands = subcmds;
	commi->HelpText = helptext;
	m_commands.insert(commi);
}

void ChatCommandTable::PrintSubcmds(Player* sender)
{
	sender->RedSystemMessage("Available subcommands:");
	set<ChatCommand*>::iterator itr = m_commands.begin();
	set<ChatCommand*>::iterator end = m_commands.end();

	for( ; itr != end; ++itr)
	{
		sender->BlueSystemMessage("%s - %s", (*itr)->InternalName, (*itr)->HelpText);
	}
}

ChatCommand* ChatCommandTable::GetCommand(char* cmdtext)
{
	if(!m_commands.size())
		return 0;
	set<ChatCommand*>::iterator itr = m_commands.begin();
	set<ChatCommand*>::iterator end = m_commands.end();

	for( ; itr != end; ++itr)
	{
		if(!stricmp((*itr)->InternalName, cmdtext))
			return (*itr);
	}

	return 0;
}

ChatCommand* ChatHandler::FindRegularCommand(char* command)
{
	set<ChatCommandTable*>::iterator itr = CommandTableStorage.begin();
	set<ChatCommandTable*>::iterator end = CommandTableStorage.end();

	for( ; itr != end; ++itr)
	{
		ChatCommand* comm = (*itr)->GetCommand(command);
		if(comm)
			return comm;
	}
	return 0;
}

void ChatHandler::HandleChatCommand(ChatCommand* cmd, Player* sender, char* args)
{
	if(!sender->CanUse(cmd->RequiredLevel))
	{
		sender->RedSystemMessage("You dont have sufficient rights to access this command!");
		return;
	}
	if(cmd->ChatProc)
	{
		bool res = (this->*(cmd->ChatProc))(sender, args);
		if(!res)
			cmd->PrintHelpText(sender);
		return;
	}
	if(cmd->subcommands)
	{
		char* subcmd = strtok(args, " ");
		if(!subcmd)
		{
			cmd->subcommands->PrintSubcmds(sender);
			return;
		}
		char* remaining = strtok(NULL, "\0");
		ChatCommand* comm = cmd->subcommands->GetCommand(subcmd);
		if(comm)
			HandleChatCommand(comm, sender, remaining);
		else
			cmd->subcommands->PrintSubcmds(sender);
	}
}

void ChatCommand::PrintHelpText(Player* target)
{
	target->GreenSystemMessage(HelpText);
}

bool ChatHandler::HandleWorldPortCmd(Player* plr, char* args)
{
	if(!args)
		return false;
	ui32 mapid = 0;
	float x, y, z;
	x = y = z = 0.0f;
	if(sscanf(args, "%u %f %f %f", &mapid, &x, &y, &z) != 4)
		return false;
	
	plr->TeleportTo(Landmark(x, y, z, mapid));
	return true;
}

bool ChatHandler::HandleNPCSpawnCmd(Player* plr, char* args)
{
	if(!args)
		return false;
	char* tmpargs = strtok(args, " ");
	char* saveargs = strtok(NULL, "\0");
	ui32 entry = atoi(tmpargs);
	CreatureDetails* det = miscmgr->GetCreatureDetails(entry);
	if(!det)
	{
		plr->RedSystemMessage("Could not find npc with entry %u", entry);
		return true;
	}
	Creature* cr = plr->GetMapHandler()->CreateCreature(entry);
	if(!cr)
	{
		plr->RedSystemMessage("Could not create the creature!");
		return true;
	}
	cr->Load(entry);
	Landmark pos = plr->GetPosition();
	cr->Spawn(pos._x, pos._y, pos._z, pos._mapid, plr->GetOrientation());
	if(sWorld.autosavenpc)
		cr->SaveToDb(true);
	else
	{
		if(saveargs && atoi(saveargs))
			cr->SaveToDb(true);
	}
	plr->GreenSystemMessage("Spawned NPC with entry '%u'", cr->GetEntry());
	return true;
}

bool ChatHandler::HandleNPCInfoCmd(Player* plr, char* /*args*/)
{
	ui64 guid = plr->GetSelection();
	Creature* cr = plr->GetMapHandler()->GetCreature(guid);
	if(!cr)
	{
		plr->RedSystemMessage("You should select a creature!");
		return true;
	}
	plr->BlueSystemMessage("Showing infos:");
	plr->SystemMessage("Entry: %u", cr->GetUI32Value(OBJECT_FIELD_ENTRY));
	plr->SystemMessage("Displayid: %u", cr->GetUI32Value(UNIT_FIELD_DISPLAYID));
	plr->SystemMessage("Faction: %u", cr->GetUI32Value(UNIT_FIELD_FACTIONTEMPLATE));
	plr->SystemMessage("SQL-ID: %u", cr->m_sqlid);
	return true;
}

bool ChatHandler::HandleNPCDeleteCmd(Player* plr, char* args)
{
	ui64 guid = plr->GetSelection();
	Creature* cr = plr->GetMapHandler()->GetCreature(guid);
	if(!cr)
	{
		plr->RedSystemMessage("You should select a creature!");
		return true;
	}
	cr->RemoveFromWorld();
	if(sWorld.autodeletenpc)
	{
		plr->GreenSystemMessage("Removed NPC from world and database");
		cr->RemoveFromDb();
	}
	else
	{
		if(args && atoi(args))
		{
			plr->GreenSystemMessage("Removed NPC from world and database");
			cr->RemoveFromDb();
		}
		else
			plr->GreenSystemMessage("Removed NPC from world");
	}
	return true;
}

bool ChatHandler::HandleNPCFlagCmd(Player* plr, char* args)
{
	if(!args)
	{
		plr->RedSystemMessage("Usage: .npc flags <new_flag>");
		return true;
	}
	ui64 guid = plr->GetSelection();
	MapHandler* handler = plr->GetMapHandler();
	if(!handler)
	{
		plr->GetSession()->Disconnect();
		return true;
	}
	Creature* cr = handler->GetCreature(guid);
	if(!cr)
	{
		plr->RedSystemMessage("This command requires you to select a NPC!");
		return true;
	}
	
	ui32 new_flags = atoi(args);
	cr->SetUI32Value(UNIT_NPC_FLAGS, new_flags);
	return true;
}

bool ChatHandler::HandleNPCFactionCmd(Player* plr, char* args)
{
	if(!args)
	{
		plr->RedSystemMessage("Usage: .npc faction <new_faction>");
		return true;
	}
	ui64 guid = plr->GetSelection();
	MapHandler* handler = plr->GetMapHandler();
	if(!handler)
	{
		plr->GetSession()->Disconnect();
		return true;
	}
	Creature* cr = handler->GetCreature(guid);
	if(!cr)
	{
		plr->RedSystemMessage("This command requires you to select a NPC!");
		return true;
	}

	ui32 new_faction = atoi(args);
	cr->SetUI32Value(UNIT_FIELD_FACTIONTEMPLATE, new_faction);
	return true;
}

bool ChatHandler::HandleNPCEmoteCmd(Player* plr, char* args)
{
	if(!args)
	{
		plr->RedSystemMessage("Usage: npc emote <new_emote>");
		return true;
	}
	ui64 guid = plr->GetSelection();
	MapHandler* handler = plr->GetMapHandler();
	if(!handler)
	{
		plr->GetSession()->Disconnect();
		return true;
	}
	Creature* cr = handler->GetCreature(guid);
	if(!cr)
	{
		plr->RedSystemMessage("This command requires you to select a NPC!");
		return true;
	}

	ui32 new_emote = atoi(args);
	cr->SetUI32Value(UNIT_NPC_EMOTESTATE, new_emote);
	cr->SaveToDb(false);
	return true;
}

bool ChatHandler::HandleNPCMovespCmd(Player* plr, char* args)
{
	if(!args)
	{
		plr->RedSystemMessage("Usage: npc emote <new_emote> <0, 1>");
		return true;
	}
	ui64 guid = plr->GetSelection();
	MapHandler* handler = plr->GetMapHandler();
	if(!handler)
	{
		plr->GetSession()->Disconnect();
		return true;
	}
	Creature* cr = handler->GetCreature(guid);
	if(!cr)
	{
		plr->RedSystemMessage("This command requires you to select a NPC!");
		return true;
	}
	ui64 cguid = cr->m_sqlid;
	Landmark pp = plr->GetPosition();
	Landmark cp = cr->GetPosition();
	CharacterDatabase.Query("Update creature_spawns set pos_x = %f , pos_y = %f , pos_z = %f orientation = %f where `index` = %I64u", 
		pp._x, pp._y, pp._z, plr->GetOrientation(), cguid);
	if(atoi(args))
		cr->MoveTo(pp._x, pp._y, pp._z);

	return true;
}

bool ChatHandler::HandleWayPointAdd(Player* plr, char* args)
{
	if(!plr)
		return true;
	ui64 guid = plr->GetSelection();
	MapHandler* handler = plr->GetMapHandler();
	if(!handler)
	{
		plr->GetSession()->Disconnect();
		return true;
	}
	Creature* cr = handler->GetCreature(guid);
	if(!cr)
	{
		plr->RedSystemMessage("This command requires you to select a NPC!");
		return true;
	}
	QResult* res = CharacterDatabase.SafeQuery("select MAX(`index`) from creature_waypoint where sqlid = %u", cr->m_sqlid);
	ui32 index = 0;
	if(!res || !res->NumRows())
	{
		if(res)
			delete res;
	}
	else
	{
		if(res->Fetch()[0].GetChar() != 0)
			index = res->Fetch()[0].GetUI32() + 1;
		delete res;
	}
	CharacterDatabase.Query("insert into creature_waypoint values('%u', '%u', '%f', '%f', '%f')", cr->m_sqlid, index, plr->GetPosition()._x,
		plr->GetPosition()._y, plr->GetPosition()._z);
	plr->BlueSystemMessage("Waypoint added!");
	return true;
}

bool ChatHandler::HandleDebugMove(Player* plr, char* args)
{
	Creature* cr = plr->GetSelectedCreature();
	if(!cr)
	{
		plr->RedSystemMessage("This command requires you to select a NPC!");
		return true;
	}
	if(args)
		cr->MoveTo(plr->GetPosition()._x, plr->GetPosition()._y, plr->GetPosition()._z, true);
	else
		cr->MoveTo(plr->GetPosition()._x, plr->GetPosition()._y, plr->GetPosition()._z);
	plr->GreenSystemMessage("Creature %I64u moves to your position!", cr->GetGUID());
	return true;
}

bool ChatHandler::HandleModifySpeed(Player* plr, char* args)
{
	if(!args)
		return false;
	Unit* chg = 0;
	Player* target = plr->GetSelectedPlayer();
	Creature* target2 = plr->GetSelectedCreature();
	if(!target && !target2)
		chg = plr;
	else if(target)
		chg = target;
	else if(target2)
		chg = target2;
	if(!chg)
		return false;
	chg->SetSpeed(1, atof(args));
	chg->SetSpeed(2, atof(args));
	chg->SetSpeed(3, atof(args));
	chg->SetSpeed(4, atof(args));
	chg->SetSpeed(5, atof(args));
	if(target)
		target->BlueSystemMessage("%s changed your speed to %f", plr->GetName(), atof(args));
	return true;
}

bool ChatHandler::HandleCheatFly(Player* plr, char* args)
{
	Player* target = plr->GetSelectedPlayer();
	if(!target)
		target = plr;
	target->TurnFlyMode(true);
	target->GreenSystemMessage("%s enabled flymode on you!", plr->GetName());
	return true;
}

bool ChatHandler::HandleWayPointActivate(Player* plr, char* args)
{
	Creature* target = plr->GetSelectedCreature();
	if(!target)
	{
		plr->RedSystemMessage("You need to select a NPC!");
		return true;
	}
	//target->UnitGoneActive();
	return true;
}

bool ChatHandler::HandleRecallPort(Player* plr, char* args)
{
	if(!args)
		return false;
	RecallEntry* entry = miscmgr->GetRecallLocation((const char*)args);
	if(!entry)
	{
		plr->RedSystemMessage("Could not find location '%s' in database!", args);
		return true;
	}

	entry->TeleportPlayer(plr);
	plr->GreenSystemMessage("Teleported to location '%s'", args);
	return true;
}

bool ChatHandler::HandleRecallAdd(Player* plr, char* args)
{
	if(!args)
		return false;
	RecallEntry* entry = new RecallEntry;
	entry->Position = plr->GetPosition();
	entry->name = new char[strlen(args) + 1];
	strcpy(entry->name, args);
	miscmgr->AddRecallLocation(entry);
	CharacterDatabase.Query("Insert into recall VALUES ( NULL , \"%s\", '%u', '%f', '%f', '%f')", args, 
		entry->mapid, entry->x, entry->y, entry->z);
	plr->GreenSystemMessage("Added recall location '%s' with id '%u' to the database!", entry->name, entry->id);
	return true;
}

bool ChatHandler::HandleModifyMoney(Player* plr, char* args)
{
	if(!args)
		return false;
	i32 mon = atoi(args);
	mon > 0 ? plr->AddMoney(mon) : plr->SubtractMoney(abs(mon));
	plr->GreenSystemMessage("%s %u copper to %s", mon > 0 ? "Added" : "Subtracted", abs(mon), plr->GetName());
	return true;
}

bool ChatHandler::HandleAnnounce(Player* plr, char* args)
{
	char msg[] = { "|cff0000ffTest" };
	RealmPacket chat(SMSG_MESSAGECHAT);
	BuildChatPacket(msg, plr->GetGUID(), CHAT_MSG_SAY, 0, &chat);
	plr->GetSession()->SendPacket(&chat);
	return true;
}

bool ChatHandler::HandleNpcPower(Player* plr, char* args)
{
	plr->SetUI32Value(UNIT_FIELD_LEVEL, atoi(args));
	return true;
}

bool ChatHandler::HandleGameObjectReloadTele(Player* plr, char*)
{
	miscmgr->ReloadGameObjectTeleport();
	sWorld.SendWorldScreenText("|cffff0000%s|r reloaded table |cff0000ff'gameobject_teleport'|r!", plr->GetName());
	return true;
}

#define GetSelectedGoRet(plr, go) go = plr->m_selectedGo; if(!go) { plr->RedSystemMessage("Ein Gameobject muss mit .go select selektiert werden!"); return true; }

bool ChatHandler::HandleGameObjectByte2(Player* plr, char* args)
{
	if(!args)
		return false;
	GameObject* obj;
	GetSelectedGoRet(plr, obj);
	obj->SetByte(GAMEOBJECT_BYTES_1, 2, atoi(args));
}

bool ChatHandler::HandleGameObjectByte3(Player* plr, char* args)
{
	if(!args)
		return false;
	GameObject* obj;
	GetSelectedGoRet(plr, obj);
	obj->SetByte(GAMEOBJECT_BYTES_1, 3, atoi(args));
}

bool ChatHandler::HandleGameObjectState(Player* plr, char* args)
{
	if(!args)
		return false;
	GameObject* obj;
	GetSelectedGoRet(plr, obj);
	obj->SetByte(GAMEOBJECT_BYTES_1, 0, atoi(args));
}

bool ChatHandler::HandleGameObjectFlags(Player* plr, char* args)
{
	if(!args)
		return false;
	GameObject* obj;
	GetSelectedGoRet(plr, obj);
	obj->SetUI32Value(GAMEOBJECT_FLAGS, atoi(args));
}

bool ChatHandler::HandleGameObjectSelect(Player* plr, char* args)
{
	set<Object*>::iterator itr = plr->GetInRangeSetBegin();
	GameObject* pObj = 0;
	float distance = 0.0f;
	for( ; itr != plr->GetInRangeSetEnd(); ++itr)
	{
		if((*itr)->GetTypeId() == TYPEID_GAMEOBJECT)
		{
			if(!pObj)
			{
				pObj = (GameObject*)*itr;
				distance = Useable::GetDistance(pObj, plr);
			}
			else
			{
				if(Useable::GetDistance(*itr, plr) < distance)
				{
					pObj = (GameObject*)*itr;
					distance = Useable::GetDistance(pObj, plr);
				}
			}
		}
	}
	if(pObj == 0)
	{
		plr->m_selectedGo = 0;
		plr->RedSystemMessage("Kein gameobject gefunden!");
		return true;
	}
	else
	{
		plr->m_selectedGo = pObj;
		plr->GreenSystemMessage("Selektiertes Gameobject: %s - Entfernung: %03.02f", pObj->GetInfo()->name, distance);
		return true;
	}
}